<?php
	require_once dirname(BASEPATH) . '/application/models/local/data/_init_model.php';
	
	class mod_carousel extends _init_model{
		public $CAR_ID;
		public $CAR_S_DATETIME;
		public $CAR_E_DATETIME;		
		public $CAR_IMG;
		public $CAR_DESC;
		
		function mod_carousel(){
			parent::__construct();		
			$this->tableName = 'carousel';
			
			$this->load->library('custom_datetime');
			$this->CAR_S_DATETIME = $this->custom_datetime->current_datetime();	
		}		
		
		function get_usable_carousel($random=true){
			if ($random)
				$this->db->order_by('CAF_ID', 'random');
			return $this->get_carousel_list();
		}
		function get_carousel_list(){			
			$this->db->where('CAR_IMG !=', '');
			$this->db->order_by('CAR_ID', 'DESC');
			$query = $this->db->get($this->tableName);
			$return = $query->result_array();
			
			if (count($return) > 0){
				$this->load->library('define/directory_path');
				$this->load->library('custom_datetime');
				
				for ($i=0; $i<count($return); $i++){
					$return[$i]['CAR_IMG'] = $this->directory_path->img_carousel_path . $return[$i]['CAR_IMG'];					
					$return[$i]['CAR_REMAIN'] = $this->custom_datetime->date_diff($return[$i]['CAR_S_DATETIME'], $return[$i]['CAR_E_DATETIME']);	
				}
			}
			
			return $return;		
		}
		function get_single_carousel($carousel_id){			
			$this->db->where('CAR_IMG !=', '');
			$this->db->where('CAR_ID', $carousel_id);
			$query = $this->db->get($this->tableName);
			
			if ($query->num_rows() > 0){
				$row = $query->row_array(0);
				
				$this->load->library('define/directory_path');
				$row['CAR_IMG'] = $this->directory_path->img_carousel_path . $row['CAR_IMG'];
				
				$this->load->library('custom_datetime');
				$row['CAR_REMAIN'] = $this->custom_datetime->date_diff($row['CAR_S_DATETIME'], $row['CAR_E_DATETIME']);
				
				return $row;
			} else
				return null;
		}
		function get_carousel_filename($carousel_id){
			$this->db->select('CAR_IMG');
			$this->db->where('CAR_ID', $carousel_id);
			$query = $this->db->get($this->tableName);
			
			if ($query->num_rows() > 0)
				return $query->row(0)->CAR_IMG;
			else
				return null;
		}	
		
		function set_carousel_state($carousel_id, $state){
			$this->db->where('CAR_ID', $carousel_id);
			if ($state)
				$state = 1;
			else
				$state = 0;
			return $this->db->update($this->tableName, array('CAR_STATE' => $state));
		}
	
		function add(){		
			$data = array(
				'CAR_S_DATETIME' => $this->CAR_S_DATETIME,
				'CAR_E_DATETIME' => $this->CAR_E_DATETIME,				
				'CAR_IMG' => $this->CAR_IMG,
				'CAR_DESC' => $this->CAR_DESC
			);
			return $this->db->insert($this->tableName, $data);
		}
		function edit($carousel_id){			
			$data = array();
			if (!empty($this->CAR_S_DATETIME))
				$data['CAR_S_DATETIME'] = $this->CAR_S_DATETIME;
			if (!empty($this->CAR_E_DATETIME))
				$data['CAR_E_DATETIME'] = $this->CAR_E_DATETIME;
			if (!empty($this->CAR_DESC))
				$data['CAR_DESC'] = $this->CAR_DESC;
				
			if (count($data) > 0){
				$this->db->where('CAR_ID', $carousel_id);
				return $this->db->update($this->tableName, $data);
			} else
				return true;		
		}
		function remove($carousel_id){
			$filename = $this->get_carousel_filename($carousel_id);
			if (!is_null($filename)){
				$this->load->library('define/directory_path');
				unlink($this->directory_path->img_upload_carousel_path . $filename);
			}			
			
			$this->db->where('CAR_ID', $carousel_id);
			return $this->db->delete($this->tableName);
		}
	}
?>